//
//  ZCMoreTableViewController.m
//  BaiSi
//
//  Created by 张灿 on 16/4/27.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCMoreTableViewController.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import "ZCTagItem.h"
#import "ZCTableViewCell.h"
#import <SVProgressHUD.h>


@interface ZCMoreTableViewController ()

@property (nonatomic, strong) NSArray *tags;

@property (weak, nonatomic) AFHTTPSessionManager *mgr;

@end

@implementation ZCMoreTableViewController

static NSString * const ID = @"cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"推荐订阅";
    
    [self loadData];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCTableViewCell" bundle:nil] forCellReuseIdentifier:ID];
    
    //取消系统的分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //设置背景色,即分割线颜色(思路:取消系统分割线,重写setFrame方法,使cell之间有间距)
    self.tableView.backgroundColor = [UIColor colorWithRed:217 / 255.0 green:217 / 255.0 blue:217 / 255.0 alpha:1.0];
}

- (void)loadData
{
    //加载弹窗
    [SVProgressHUD showWithStatus:@"正在加载..."];
    
    //创建会话管理者
    self.mgr = [AFHTTPSessionManager manager];
    
    NSDictionary *dict = @{
                           @"a":@"tag_recommend",
                           @"action":@"sub",
                           @"c":@"topic"
                           };
    
    [self.mgr GET:@"http://api.budejie.com/api/api_open.php" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.tags = [ZCTagItem mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.tableView reloadData];
        
        //加载完成取消弹窗
        [SVProgressHUD dismiss];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //加载失败取消弹窗
        [SVProgressHUD dismiss];
        
    }];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //返回上一个界面,即此界面即将消失的时候也要取消弹窗
    [SVProgressHUD dismiss];
    //并且取消网络请求
    [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.tags.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    ZCTagItem *item = self.tags[indexPath.row];
    
    cell.item = item;

    return cell;
}

//设置cell高度60
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 61;
}



@end
